package com.fast.admin.controller;

import com.fast.admin.common.utils.Const;
import com.fast.common.base.BaseController;
import com.fast.common.util.R;
import com.fast.common.util.emuns.StatusCode;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.ExpiredCredentialsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.omg.CORBA.portable.ApplicationException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Title：后台管理控制器
 * @author：ylc
 * @date：2018/7/23 14:51
 * @version：1.0
 **/
@Controller
@Slf4j
public class AdminController extends BaseController {

    @GetMapping(value = "/login")
    public String loginView() {
        return "login";
    }

    @GetMapping(value = "/index")
    public String index() {
        return "index";
    }

    /**
     * 登录
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping(value = "/toLogin")
    @ResponseBody
    public R toLogin(String username, String password, String captcha) {
        String code = (String) request().getSession().getAttribute(Const.VERIFY_CODE);
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            return R.error(StatusCode.LOGIN_FAILED.getCode(), StatusCode.LOGIN_FAILED.getMessage());
        } else if (StringUtils.isEmpty(captcha) || (StringUtils.isNotEmpty(captcha) && !captcha.equals(code))) {
            return R.error(StatusCode.CAPTCHA_ERROR.getCode(), StatusCode.CAPTCHA_ERROR.getMessage());
        } else {
            Subject currentUser = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            try {
                if (!currentUser.isAuthenticated()) {
                    currentUser.login(token);
                } else {
                    currentUser.logout();
                    currentUser.login(token);
                }
            } catch (IncorrectCredentialsException e) {
                //用户名或密码错误
                return R.error(StatusCode.LOGIN_FAILED.getCode(), StatusCode.LOGIN_FAILED.getMessage());
            } catch (ExpiredCredentialsException e) {
                //帐号已过期
                log.error("登录失败{}", e.getMessage());
                R.error(StatusCode.LOGIN_FAILED.getCode(), StatusCode.LOGIN_FAILED.getMessage());
            } catch (UnknownAccountException e) {
                //帐号不存在
                log.error("登录失败{}", e.getMessage());
                R.error(StatusCode.NOT_FOUND.getCode(), StatusCode.NOT_FOUND.getMessage());
            } catch (Exception e) {
                log.error("登录失败{}", e.getMessage());
                R.error(StatusCode.BAD_REQUEST.getCode(), StatusCode.BAD_REQUEST.getMessage());
            }
        }
        return R.ok();
    }

    /**
     * 退出登录
     */
    @ApiOperation(value = "退出登录")
    @GetMapping(value = "/logout")
    public R logout() {
        Subject currentUser = SecurityUtils.getSubject();
        currentUser.logout();
        return R.ok();
    }
}
